package com.wwl.deliveryappointmentbackend.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String getCurrentDateTime() {
        return formatDateTime(new Date());
    }

    public static String formatDateTime(Date date) {
        return formatDateTime(date, DEFAULT_DATE_FORMAT);
    }

    public static String formatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date parseDateTime(String dateTimeStr) throws ParseException {
        return parseDateTime(dateTimeStr, DEFAULT_DATE_FORMAT);
    }

    public static Date parseDateTime(String dateTimeStr, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateTimeStr);
    }

    public static Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        // 获取当前日期时间
        String currentDateTime = getCurrentDateTime();
        System.out.println("Current DateTime: " + currentDateTime);

        // 格式化日期时间
        String formattedDateTime = formatDateTime(new Date());
        System.out.println("Formatted DateTime: " + formattedDateTime);

        // 解析日期时间
        try {
            Date parsedDateTime = parseDateTime("2023-06-01 12:30:45");
            System.out.println("Parsed DateTime: " + parsedDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 添加天数
        Date now = new Date();
        Date futureDate = addDays(now, 7);
        System.out.println("Future Date after 7 days: " + formatDateTime(futureDate));
    }
}
